home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 109_01.zip / SCRUB.C < prev    next >
Text File  |  1993-06-26  |  2KB  |  79 lines

  1. /*
  2.     Program to copy a file deleting:
  3.         all non tab,lf,cr,ff control chars
  4. */
  5.  
  6. /*
  7.     Macros for constant definitions
  8. */
  9.  
  10. #define ERROR -1    /* error flag returned by buffered i/o routines */
  11. #define EOFF -1        /* end of file marker returned by getc() */
  12. #define EOF 0x1A    /* CP/M's end file char for ascii files */
  13. #define NOFILE -1    /* no such file indication given by fopen() */
  14.  
  15. /*
  16.     Argument vector indices
  17. */
  18.  
  19. #define FROM_FILE 1
  20. #define TO_FILE   2
  21.  
  22. /*
  23.     main to open the files for scrub()
  24.     and handle invocation errors.
  25. */
  26.  
  27. main(argc,argv)
  28.   int argc;
  29.   char *argv[];
  30.   {
  31.   int fdin,fdout;
  32.   char inbuf[134],outbuf[134];
  33.  
  34.   if( argc != 3 ) {
  35.     puts("Correct invocation form is:\n\n");
  36.     puts(" SCRUB <from file> <to file>\n");
  37.     }
  38.   else if( (fdin = fopen(argv[FROM_FILE],inbuf)) == NOFILE )
  39.     printf("No such file %s\n",argv[FROM_FILE]);
  40.   else if( (fdout  = fcreat(argv[TO_FILE],outbuf)) == ERROR )
  41.     printf("Can't open %s\n",argv[TO_FILE]);
  42.   else
  43.     scrub(inbuf,outbuf);
  44.   exit();
  45.   }
  46.  
  47. /*
  48.     procedure scrub -- copy file to file deleting unwanted ctrl chars
  49. */
  50.  
  51. scrub(filein,fileout)
  52.   char filein[];    /* the input file buffer */
  53.   char fileout[];    /* the output file buffer */
  54.   {
  55.   int c;        /* 1 char buffer */
  56.   unsigned killed;    /* numbers of bytes deleted */
  57.  
  58.   killed = 0;
  59.   while( (c = getc(filein)) != EOFF  && c != EOF )
  60.     if( c >= ' ' && c < '\177' ) /* is a visable character */
  61.       putc(c,fileout);
  62.     else
  63.       switch(c) {
  64.         case '\r':
  65.         case '\n':
  66.         case '\t':
  67.         case '\f':    putc(c,fileout);    /* ok control chars */
  68.             break;
  69.  
  70.         default:    killed++;
  71.             break; /* ignore it */
  72.         }
  73.   putc(EOF,fileout); /* sent textual end of file */
  74.   if( fflush(fileout) == ERROR)
  75.     exit(puts("output file flush error\n"));
  76.   printf("%u characters were deleted\n",killed);
  77.   }    /* end scrub */
  78.  
  79.